import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
const Login=()=> import('../views/Login.vue')
const Layout=()=> import('../views/Layout.vue')
const Menu=()=> import('../views/menu/index.vue')
const Role=()=> import('../views/role/index.vue');
const Admin=()=> import('../views/user/index.vue');
const Category=()=> import('../views/category/index.vue');
const Specs=()=> import('../views/specs/index.vue');
const Members=()=> import('../views/member/index.vue')
const Goods=()=> import('../views/goods/index.vue');
const Banner=()=> import('../views/banner/index.vue');
const Seckill=()=> import('../views/seckill/index')
const Index=()=> import('../views/index')
Vue.use(VueRouter)

const routes = [
  {
    path:'/login',
    component:Login,
    meta:{
      title:'欢迎登录'
    },
  },
  {
    path:'/',
    component:Layout,
    // 以上是父路由
    children:[
      {
        path:'/',
        component:Index
      },
      {
        path:'/menu',
        component:Menu,
        meta:{
          title:'菜单管理'
        },
      },
      {
        path:'/role',
        component:Role,
        meta:{
          title:'角色管理'
        },
        
      },
      {
        path:'/user',
        component:Admin,
        meta:{
          title:'管理员管理'
        },
      },
      {
        path:'/category',
        component:Category,
        meta:{
          title:'商品分类'
        },
      },
      {
        path:'/specs',
        component:Specs,
        meta:{
          title:'商品规格'
        },
      },
      {
        path:'/member',
        component:Members,
        meta:{
          title:'商品管理'
        },
      },
      {
        path:'/goods',
        component:Goods,
        meta:{
          title:'会员管理'
        },
      },
      {
        path:'/banner',
        component:Banner,
        meta:{
          title:'菜单轮播图管理管理'
        },
      },
      {
        path:'/seckill',
        component:Seckill,
        meta:{
          title:'秒杀活动'
        },
      }
    ]

  }
]

const router = new VueRouter({
  routes
})
//全局前置首位。没登录的话只能看login和首页
router.beforeEach((to,from,next)=>{
  if(store.state.userInfo.token){
    next();
  }else{
    //没登录的话跳转
    if(to.path=='/login'||to.path=='/'){
      next()
    }else{
      router.push('/login')
    }
  }
})
export default router
